package cn.wolfcode.redis.service.impl;

import cn.wolfcode.enums.RedisKey;
import cn.wolfcode.redis.service.IRankRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RankRedisServiceImpl implements IRankRedisService {
    @Autowired
    private StringRedisTemplate template;
    @Override
    public void initRankData(String authorName, Long count) {
        //获取key
        String key = RedisKey.RANK_STATIS.getKey();
        //往redis中保存数据
        template.opsForZSet().add(key,authorName,count);
    }

    @Override
    public Set<String> searchRankData() {
        String key = RedisKey.RANK_STATIS.getKey();
        //获取排行数据
        Set<String> values = template.opsForZSet().reverseRange(key, 0, -1);
        return values;
    }

    @Override
    public void increaseRankData(String authorName) {
        String key = RedisKey.RANK_STATIS.getKey();
        //将发帖数量加1
        template.opsForZSet().incrementScore(key,authorName,1);
    }
}
